

	function onYouTubePlayerReady(playerId) {    	
        ytplayer = document.getElementById("myytplayer");
    }
    
    function play(){
    	if(ytplayer){
    		ytplayer.playVideo();
    		$(".video-container .controls .pause:first").focus();
    	}
    }
    
    function pause(){
    	if(ytplayer){
    		ytplayer.pauseVideo();
    		$(".video-container .controls .play:first").focus();
    	}
    }
    
    function stop(){
    	if(ytplayer){
    		ytplayer.stopVideo();
    	}
    }
    
    function back(){
    	if(ytplayer){
    		ytplayer.seekTo(0);
    	}
    }

    function incrementVolumeBy(increment){
    	ytplayer.setVolume(ytplayer.getVolume()+increment)
    }
    
    function load(vId, title, description){
	    if(ytplayer){
	    	ytplayer.loadVideoById(vId);
	    	$(".video-container h3").html(title);
	    	$(".video-container p").html(description);
	    }
    }

    

function search(){
    	var q = $('#q').val();
    	var url = "http://gdata.youtube.com/feeds/api/videos?q="+q+"&start-index=1&max-results=10&v=2&alt=jsonc";
    	var directive = {
    		"div.videoResult" : {
    			"v <- data.items" : {
    				"h3" : "#{v.title}",
    				"p" : "#{v.description}",
    				"input.vid[type='hidden']@value" : "#{v.id}",
    				"img@src" : "#{v.thumbnail.hqDefault}" 
    			}
    		}		
    	};
    	$(".videos-container .hint").hide();
		$(".videos-container .loading").show();
    	$.getJSON(url,
    			function(data){
    				$("#results").render(data, directive)
    				$("#results").show();
    				var bs = $("div.videoResult").find("input[type='button']");
    				for (i=0;i<bs.length;i++){
    					$(bs[i]).click(function(){
							var videoContainer = $(this).parents("div.videoResult:first");
    						load(
    							videoContainer.find("input.vid[type='hidden']").val(),//video id
    							videoContainer.find("h3").html(),//title
    							videoContainer.find("p").html()//description
    							
    						);
							$(".video-container .wu-action:first").focus();
							window.scrollTo(0,0);
						});
    				}
    				$("#results .videoResult:first").focus();
    				$(".videos-container .loading").hide();
    				
    			}		
    	);
    }
